
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author khang
 */
public class Enemy2 {
    private int iwidth,iheight,iX,iY,timefire;;
    private Sprite Enemy,power,death;
    private Sprite[] Bullet1,Bullet2;
    private int[] isShoot1,isShoot2;
    private int health,havePower,isdeath,nBullet1,nBullet2;
    private boolean checkDeath;
    private int[] turnFire2;
    Sound sound;
    public Enemy2(LayerManager lm,Sound s,int width,int height){
        iwidth=width;
        iheight=height;
        health=2;
        isdeath=0;
        nBullet1=6;
        Bullet1=new Sprite[nBullet1];
        isShoot1=new int[nBullet1];
        nBullet2=2;
        Bullet2=new Sprite[nBullet2];
        isShoot2=new int[nBullet2];
        checkDeath=false;
        turnFire2=new int[nBullet1];
        sound=s;
        for(int i=0;i<nBullet1;i++){
            turnFire2[i]=0;
        }
        try {
            Enemy=new Sprite(Image.createImage("/Kanako.png"), 32, 32);
            //int[]frameSequence={0,1,2,3};
            //Enemy.setFrameSequence(frameSequence);
            Enemy.defineReferencePixel(Enemy.getWidth()/2, Enemy.getHeight()/2);
            Enemy.setVisible(false);
            power=new Sprite(Image.createImage("/DevildomTorchOrrerriesSun.png"), 32, 32);
            power.setFrame(3);           
            power.defineReferencePixel(power.getWidth()/2, power.getHeight()/2);
            power.setVisible(false);
            death=new Sprite(Image.createImage("/DevildomTorchOrrerriesSun.png"), 32, 32);
            int[]frameSequence2={0,1,2};
            death.setFrameSequence(frameSequence2);
            death.defineReferencePixel(death.getWidth()/2, death.getHeight()/2);
            death.setVisible(false);
            for(int i=0;i<nBullet1;i++){
                Sprite bullet=new Sprite(Image.createImage("/Bullet1.png"));
                Bullet1[i]=bullet;
                Bullet1[i].defineReferencePixel(Bullet1[i].getWidth()/2, Bullet1[i].getHeight()/2);
                Bullet1[i].setVisible(false);
                isShoot1[i]=0;
                lm.append(Bullet1[i]);
            }
            for(int i=0;i<nBullet2;i++){
                Sprite bullet=new Sprite(Image.createImage("/bullet2.png"),40,40);
                Bullet2[i]=bullet;
                int[]frameSequence3={0,1,2};
                Bullet2[i].setFrameSequence(frameSequence3);
                Bullet2[i].defineReferencePixel(Bullet2[i].getWidth()/2, Bullet2[i].getHeight()/2);
                Bullet2[i].setVisible(false);
                isShoot2[i]=0;
                lm.append(Bullet2[i]);
            }
            lm.append(death);
            lm.append(Enemy);
            lm.append(power);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        iX=Enemy.getRefPixelX();
        iY=Enemy.getRefPixelY();
    }
    public void enemyFireType1(int scrY){
        timefire--;
        iX=Enemy.getRefPixelX();
        iY=Enemy.getRefPixelY();
        for(int i=0;i<nBullet1;i++){
            if(Bullet1[i].getRefPixelY()>scrY+iheight){
                Bullet1[i].setVisible(false);
                isShoot1[i]=0;
            }
        }
        for(int i=0;i<nBullet1;i++){
            if(isShoot1[i]!=0){
                switch(isShoot1[i]){
                    case 1:Bullet1[i].move(0, 2);break;
                    case 2:Bullet1[i].move(-1, 2);break;
                    case 3:Bullet1[i].move(1, 2);break;
                }
            }
        }
        if(timefire<=0&&scrY<Enemy.getRefPixelY()+Enemy.getHeight()/2&&!checkDeath){
            for(int i=0;i<nBullet1;i++){
                if(isShoot1[i]==0){
                    Bullet1[i].setRefPixelPosition(iX, iY);
                    Bullet1[i].setVisible(true);
                    isShoot1[i]=1;
                    for(int j=i+1;j<nBullet1;j++){
                        if(isShoot1[j]==0){
                            Bullet1[j].setRefPixelPosition(iX, iY);
                            Bullet1[j].setVisible(true);
                            isShoot1[j]=2;
                            for(int k=j+1;k<nBullet1;k++){
                                if(isShoot1[k]==0){
                                    Bullet1[k].setRefPixelPosition(iX, iY);
                                    Bullet1[k].setVisible(true);
                                    isShoot1[k]=3;
                                    timefire=70;
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    public void enemyFireType2(int scrY){
        timefire--;
        iX=Enemy.getRefPixelX();
        iY=Enemy.getRefPixelY();
        for(int i=0;i<nBullet1;i++){
            if(Bullet1[i].getRefPixelY()>scrY+iheight){
                Bullet1[i].setVisible(false);
                isShoot1[i]=0;
            }
        }       
        for(int i=0;i<nBullet1;i++){
            if(Bullet1[i].getRefPixelX()<iX-50){
                turnFire2[i]=1;
            }
            if(Bullet1[i].getRefPixelX()>iX+50){
                turnFire2[i]=0;
            }
            if(isShoot1[i]!=0){
                switch(turnFire2[i]){
                    case 0:{
                        Bullet1[i].move(-2, 4);
                    }break;
                    case 1:{
                        Bullet1[i].move(2, 4);
                    }break;
                }
            }
        }
        if(timefire<=0&&scrY<Enemy.getRefPixelY()+Enemy.getHeight()/2&&!checkDeath){
            for(int i=0;i<nBullet1;i++){
                if(isShoot1[i]==0){
                    Bullet1[i].setRefPixelPosition(iX, iY);
                    Bullet1[i].setVisible(true);
                    isShoot1[i]=1;
                    timefire=20;
                    break;
                }
            }
        }
    }
    public void enemyFireType3(Sprite player,int scrY){
        timefire--;
        iX=Enemy.getRefPixelX();
        iY=Enemy.getRefPixelY();
        for(int i=0;i<nBullet2;i++){
            if(Bullet2[i].getRefPixelY()>scrY+iheight){
                Bullet2[i].setVisible(false);
                isShoot2[i]=0;
            }
        } 
        for(int i=0;i<nBullet2;i++){
            if(isShoot2[i]!=0){
                if(Bullet2[i].getRefPixelX()==player.getRefPixelX()){
                    Bullet2[i].move(0, 3);
                }
                if(Bullet2[i].getRefPixelX()<player.getRefPixelX()){
                    Bullet2[i].move(1, 3);
                }
                if(Bullet2[i].getRefPixelX()>player.getRefPixelX()){
                    Bullet2[i].move(-1, 3);
                }
                Bullet2[i].nextFrame();
            }
        }
        if(timefire<=0&&scrY<Enemy.getRefPixelY()+Enemy.getHeight()/2&&!checkDeath){
            for(int i=0;i<nBullet2;i++){
                if(isShoot2[i]==0){
                    Bullet2[i].setRefPixelPosition(iX, iY);
                    Bullet2[i].setVisible(true);
                    isShoot2[i]=1;
                    timefire=70;
                    break;
                }
            }
        }
    }
    public int checkFire(Sprite sprite,int health){
        for(int i=0;i<nBullet1;i++){
            if(Bullet1[i].collidesWith(sprite, true)){
                Bullet1[i].setVisible(false);
                isShoot1[i]=0;
                health--;
            }
        }
        for(int i=0;i<nBullet2;i++){
            if(Bullet2[i].collidesWith(sprite, true)){
                Bullet2[i].setVisible(false);
                isShoot2[i]=0;
                health--;
            }
        }
        return health;
    }
    public void setCondition(){
        int iX=Enemy.getRefPixelX();
        int iY=Enemy.getRefPixelY();
        if(health<=0){
            Enemy.setVisible(false);
            health=2;
            isdeath=2;
            checkDeath=true;
            Random ran=new Random();
            havePower=ran.nextInt(4);
            death.setRefPixelPosition(iX, iY);
            death.setVisible(true);
            if(havePower==1){
                power.setRefPixelPosition(iX, iY);
                power.setVisible(true);
            }
            //sound.enemyDeathSound();
        }
        if(isdeath>=0){          
            death.nextFrame();
            isdeath--;
        }
        else{
            death.setVisible(false);
        }
    }
    public int getHealth(){
        return health;
    }
    public void setHealth(int health){
        this.health=health;
    }
    public int checkPower(Sprite sprite,int pow,int scrY){
        if(power.collidesWith(sprite, true)){
            power.setVisible(false);
            pow++;
        }
        if(power.getRefPixelY()>scrY+iheight){
            power.setVisible(false);
        }
        return pow;
    }
    //di nguoc len
    public void moveEnemyType1(int scrY){
        if(Enemy.getRefPixelY()>scrY+15){
            Enemy.move(0, -1);
        }
        //Enemy.nextFrame();
    }
    //di xuong
    public void moveEnemyType2(int scrY){
        Enemy.move(0, 1);
        //Enemy.nextFrame();
        if(Enemy.getRefPixelY()-Enemy.getHeight()/2>scrY+iheight){
            Enemy.setVisible(false);
        }
    }
    public void setPosition(int x,int y){
        Enemy.setVisible(true);
        checkDeath=false;
        Enemy.setRefPixelPosition(x, y);
    }
    public Sprite getEnemy(){
        return Enemy;
    }
    public void refreshEnemy(){
        Enemy.setVisible(false);
        power.setVisible(false);
        death.setVisible(false);
        for(int i=0;i<nBullet1;i++){
            Bullet1[i].setVisible(false);
        }
        for(int i=0;i<nBullet2;i++){
            Bullet2[i].setVisible(false);
        }
    }
}
